﻿#region License
//  Distrubted Under the GNU Public License version 2 (GPLv2)
// ========================================
// 
//  QuizzMasterStudio Version 3 
//  Copyright © 2012 , The Sparks (Autism App Jam 2014)
//  All rights reserved.  
// 
// Robert Long, Zephyr Yao, Hillary Chansavang, Li Xiao, Arial Xiao
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//  The full license is also included in the root folder.
// 
// Robert Long
//  Email: rbtLong@live.com
#endregion

using System;
using System.Windows.Media.Animation;
using Core.Util;

namespace Core.RandomMovements
{
    public enum EyeMovementHorizontal
    {
        None = 0,
        Left,
        Right,
    }

    public enum EyeMovementVertical
    {
        None = 0,
        Up,
        Down,
    }

    public class EyeMovementEvtArgs : EventArgs
    {
        private readonly EyeMovementHorizontal _horizontal;
        private readonly EyeMovementVertical _vertical;

        public EyeMovementVertical Vertical { get { return _vertical; } }
        public EyeMovementHorizontal Horizontal { get { return _horizontal; } }

        internal EyeMovementEvtArgs(EyeMovementHorizontal h, EyeMovementVertical v)
        {
            _horizontal = h;
            _vertical = v;
        }

    }

    public class EyeDirectionGesture
    {
        private readonly RangedTimer _tmr = new RangedTimer(6481, 2300);
        private readonly Random _rnd = new Random();
        private readonly Storyboard _eyesLeftAnim;
        private readonly Storyboard _eyesRightAnim;
        private readonly Storyboard _eyesDownAnim;
        private readonly Storyboard _eyesUpAnim;

        public event EventHandler<EyeMovementEvtArgs> Elapsed = delegate { }; 
        public bool IsRunning { get { return _tmr.IsEnabled; }}

        public EyeDirectionGesture(Storyboard eyesLeftAnim, Storyboard eyesRightAnim, 
            Storyboard eyesDownAnim, Storyboard eyesUpAnim)
        {
            _eyesLeftAnim = eyesLeftAnim;
            _eyesRightAnim = eyesRightAnim;
            _eyesDownAnim = eyesDownAnim;
            _eyesUpAnim = eyesUpAnim;
            _tmr.Tick += _tmr_Tick;
        }

            void _tmr_Tick(object sender, EventArgs e)
            {
                var hValue = _rnd.Next(0, 6) % 3;
                var vValue = _rnd.Next(0, 6) % 3;

                switch ((EyeMovementHorizontal)hValue)
                {
                    case EyeMovementHorizontal.Left:
                        _eyesLeftAnim.Begin();
                        break;
                    case EyeMovementHorizontal.Right:
                        _eyesRightAnim.Begin();
                        break;
                }

                switch ((EyeMovementVertical)vValue)
                {
                    case EyeMovementVertical.Down:
                        _eyesDownAnim.Begin();
                        break;
                    case EyeMovementVertical.Up:
                        _eyesUpAnim.Begin();
                        break;
                }

                Elapsed(this, new EyeMovementEvtArgs(
                    (EyeMovementHorizontal)hValue, 
                    (EyeMovementVertical)vValue));
            }

        public void Start()
        {
            _tmr.Start();
        }

        public void Stop()
        {
            _tmr.Stop();
        }
    }
}
